package com.cn.longdistancebusstation.scenic.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.base.BaseActivity;
import com.cn.longdistancebusstation.httpHelper.HttpHelper;
import com.cn.longdistancebusstation.httpapi.HttpService;
import com.cn.longdistancebusstation.model.Contacter;
import com.cn.longdistancebusstation.model.ListResultV;
import com.cn.longdistancebusstation.model.ScenicOrderDetails;
import com.cn.longdistancebusstation.model.ScenicOrderList;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by mucheng on 2018/3/30.
 */

public class CancleOrderDetailsActivity extends BaseActivity implements View.OnClickListener{
    private TextView mOrderStatusTextView;
    private TextView mTicketNameTextView;
    private TextView mTicketPriceTextView;
    private ImageView mOrderNoteImageView;
    private TextView mTicketIdTextView;
    private TextView mOrderTimeTextView;
    private ScenicOrderList mScenicOrderList;
    private String ticketId,status;
    private ArrayList<Contacter> mContacterInfo=new ArrayList<>();
    private RecyclerView mRecyclerView;
    private TouristAdapter mTouristAdapter;
    private ScenicOrderDetails mScenicOrderDetails;
    private String touristName,touristCardNo,touristTelephone;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cancle_order_details);
        getTitleTextView().setText("订单详情");
        initView();
        getOrderDetails();
    }


    private void initView() {
        Intent in=getIntent();
        ticketId=in.getStringExtra("orderId");
        status=in.getStringExtra("status");
        mOrderStatusTextView=(TextView)findViewById(R.id.textView_order_status);
        if("2".equals(status)){
            mOrderStatusTextView.setText("已取消");
        }
        mTicketNameTextView=(TextView)findViewById(R.id.ticket_name);
        mTicketPriceTextView=(TextView)findViewById(R.id.ticket_amount_price);
        mOrderNoteImageView=(ImageView)findViewById(R.id.order_note);
        mOrderNoteImageView.setOnClickListener(this);
        mTicketIdTextView=(TextView)findViewById(R.id.ticket_number);
        mOrderTimeTextView=(TextView)findViewById(R.id.order_time);
        mRecyclerView=(RecyclerView)findViewById(R.id.tourist_list);
        mTouristAdapter=new TouristAdapter();
        mRecyclerView.setAdapter(mTouristAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    }
    private void getOrderDetails() {
        showHud();
        Retrofit retrofit=new HttpHelper.HttpServiceBuilder().build().getRetrofit();
        HttpService service=retrofit.create(HttpService.class);
        Call<ListResultV<ScenicOrderDetails>> call=service.getScenicOrderDetails(ticketId);
        call.enqueue(new Callback<ListResultV<ScenicOrderDetails>>() {
            @Override
            public void onResponse(Call<ListResultV<ScenicOrderDetails>> call, Response<ListResultV<ScenicOrderDetails>> response) {
                hideHud();
                ListResultV listResultV=response.body();
                if(listResultV!=null){
                    if(listResultV.getHead().getSuccess()) {
                        Gson gson = new Gson();
                        Map<String, String> scenicMap = listResultV.getBody().getData();
                        Type type = new TypeToken<ScenicOrderDetails>() {
                        }.getType();
                        String strScenicOrderJSON = new Gson().toJson(scenicMap);
                        mScenicOrderDetails = gson.fromJson(strScenicOrderJSON, type);
                        mTicketNameTextView.setText(mScenicOrderDetails.getPltitle()+"("+mScenicOrderDetails.getTtitle()+")"+mScenicOrderDetails.getNum()+"张");
                        mTicketPriceTextView.setText(mScenicOrderDetails.getTotalPrice()+"元");
                        mTicketIdTextView.setText(mScenicOrderDetails.getOrderId());
                        mOrderTimeTextView.setText(mScenicOrderDetails.getCreateTime());
                        touristName=mScenicOrderDetails.getOrdername();
                        touristCardNo=mScenicOrderDetails.getPersonID();
                        touristTelephone=mScenicOrderDetails.getOrdertel();
                        String[] names = touristName.split(",");
                        String[] phones = touristTelephone.split(",");
                        String[] cardNos = touristCardNo.split(",");

                        for(int i=0; i<names.length; i++)
                        {
                            String strName = names[i];
                            String strPhone = phones[i];
                            String strCardNo = cardNos[i];
                            Contacter contacter = new Contacter();

                            contacter.setName(strName);
                            contacter.setTelephone(strPhone);
                            contacter.setCardNo(strCardNo);
                            mContacterInfo.add(contacter);
                        }
                        mTouristAdapter.notifyDataSetChanged();

                    }

                }

            }

            @Override
            public void onFailure(Call<ListResultV<ScenicOrderDetails>> call, Throwable t) {

            }
        });
    }


    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.order_note:
                Intent in=new Intent(CancleOrderDetailsActivity.this,ScheduleNoteActivity.class);
                startActivity(in);
                break;

        }

    }

    private class TouristAdapter extends RecyclerView.Adapter<TouristViewHolder>{
        @Override
        public TouristViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(CancleOrderDetailsActivity.this);
            View view = layoutInflater.inflate(R.layout.tourist_info_recycle_item, parent, false);
            return new TouristViewHolder(view);
        }

        @Override
        public void onBindViewHolder(TouristViewHolder holder, int position) {
            Contacter contacter=mContacterInfo.get(position);
            holder.bindData(contacter);

        }

        @Override
        public int getItemCount() {
            return mContacterInfo.size();
        }
    }

    private class TouristViewHolder extends RecyclerView.ViewHolder{
        private TextView mTouristNameTextView;
        private TextView mTouristIdcardNoTextView;
        private TextView mTouristTelephoneTextView;
        public TouristViewHolder(View itemView) {
            super(itemView);
            mTouristNameTextView=(TextView)itemView.findViewById(R.id.tourist_name);
            mTouristIdcardNoTextView=(TextView)itemView.findViewById(R.id.idcard_numbers);
            mTouristTelephoneTextView=(TextView)itemView.findViewById(R.id.telephone_number);

        }

        public void bindData(Contacter contacter) {
            mTouristNameTextView.setText(contacter.getName());
            mTouristIdcardNoTextView.setText(contacter.getCardNo());
            mTouristTelephoneTextView.setText(contacter.getTelephone());

        }
    }
}
